#ifdef DEBUG
#include <assert.h>
#endif
#include <iostream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Master.h"
#include "Messenger.h"
#include "PlatformFactory.h"
#include "Screen.h"
#include "Slave.h"

#include "Serializer.h"

#define USAGE usage(); return EXIT_FAILURE;

void usage();

void usage()
{
    printf("Usage: mouseandcat master|slave <ip address> <port> [-position <n>]\n");
}

void Test();

void Test()
{
    unsigned char buffer[8];
    short i = INT16_MIN;
    
    while (i < INT16_MAX)
    {
        short output = 0;
        Serializer::SerializeInt(i, buffer);
        Serializer::DeserializeInt(buffer, &output);
        assert(i == output);
        i++;
    }
}

int main (int argc, const char * argv[])
{
    Test();
    using std::string;
    //const char* kModeMaster = "master";
    const char* kModeSlave  = "slave";
    
    if(argc < 3)
    {
        usage();
        return EXIT_FAILURE;
    }
    
    string mode(argv[1]);
    string ip(argv[2]);
    int port(atoi(argv[3]));
    
    printf("starting mouseandcat in mode %s with address %s:%d\n", mode.c_str(), ip.c_str(), port);
    
	Screen screen = PlatformFactory::GetScreen();
	MncPoint size = screen.GetSize();

    if(mode == kModeSlave)
    {
        printf("Starting slave\n");
        Slave* slave = PlatformFactory::GetSlave(ip, port, size);
        slave->Start();
        delete slave;
    }
    else
    {
        USAGE
    }

	printf("Press any key to exit\n");
	fgetc(stdin);
    
    return EXIT_SUCCESS;
}

